#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@Author          : ricsy
@File            : context.py
@Create Time     : 2025/6/18 04:48
@Last Modified   :
@Description     :
"""
from typing import Any, Dict

from theme_customizer.base import BaseCustomizer


class ContextManager(BaseCustomizer):
    """管理模板上下文相关定制"""

    def apply(self):
        pass

    # noinspection PyUnusedLocal,PyShadowingNames
    def update_context(self, context):
        """更新模板上下文"""
        context.update(
            {
                "logger": self._build_logger_info(),
            }
        )
        context.update(
            {
                "project": self.app.config.project,
                "release": self.app.config.release,
                "theme": self._build_theme_info(),
            }
        )

    def _build_logger_info(self) -> Dict[str, Any]:
        return {  # lambda 可避免污染模板输出
            "debug": lambda msg: self.logger.debug(msg) or "",
            "info": lambda msg, _type=None: self.logger.info(msg, _type) or "",
            "warning": lambda msg: self.logger.warning(msg) or "",
            "error": lambda msg: self.logger.error(msg) or "",
        }

    def _build_theme_info(self) -> Dict[str, Any]:
        """构建主题信息字典"""
        return {
            "is_book": self.app.config.html_theme == self.SPHINX_BOOK_THEME_NAME,
            "name": self.app.config.html_theme,
            "html_last_updated_fmt": self.app.config.html_last_updated_fmt,
        }
